package com.sanpu.kxe.business.login.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatCheckBox;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseEditActivity;
import com.sanpu.kxe.business.login.contract.RegisterContract;
import com.sanpu.kxe.business.login.presenter.RegisterPresenter;
import com.sanpu.kxe.business.main.provider.MainOpenProvider;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.module.dialog.MaterialDialog;
import com.sanpu.kxe.module.dialog.interfaces.OnBtnClickL;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.module.specialunit.OnClickableSpanListener;
import com.sanpu.kxe.module.specialunit.SimplifySpanBuild;
import com.sanpu.kxe.module.specialunit.unit.SpecialClickableUnit;
import com.sanpu.kxe.module.widget.EditTextWithDel;
import com.sanpu.kxe.utils.SoftInputUtil;
import com.sanpu.kxe.utils.ToastUtil;
import com.sanpu.kxe.utils.VerifyUtil;

import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Description：注册
 * Created by 薛勇军 on 2018/6/14.
 */

public class RegisterActivity extends BaseEditActivity implements RegisterContract.View {

    @Bind(R.id.user_name)
    EditTextWithDel vUserName;
    @Bind(R.id.password)
    EditTextWithDel vPassword;
    @Bind(R.id.comfirmPwd)
    EditTextWithDel comfirmPwd;
    @Bind(R.id.check_code)
    EditTextWithDel vVerificationCode;
    @Bind(R.id.send_code)
    TextView vSendCode;
    @Bind(R.id.invite)
    EditTextWithDel vInvite;
    @Bind(R.id.register)
    RoundTextView vRegister;
    @Bind(R.id.sure)
    AppCompatCheckBox vSure;
    @Bind(R.id.legal)
    TextView vLegal;

    private MaterialDialog mDialog;

    private RegisterContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        super.initView();
        SpecialClickableUnit clickableUnit = new SpecialClickableUnit(new OnClickableSpanListener() {
            @Override
            public void onClick(TextView tv, String clickText) {
                MainOpenProvider.getInstance().openBrowserX5Activity(mContext, UrlConstants.LEGAL, true);

            }
        });
        clickableUnit.setSpecialText("《银信通用户使用协议》");
        clickableUnit.setNormalTextColor(ContextCompat.getColor(mContext, R.color.color_base));
        clickableUnit.setPressTextColor(ContextCompat.getColor(mContext, R.color.base_press_color));
        SimplifySpanBuild build = new SimplifySpanBuild(this, vLegal, "同意");
        build.appendSpecialUnit(clickableUnit);
        vLegal.setText(build.build());
        mPresenter = new RegisterPresenter(this);
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setListener() {
        super.setListener();
        RxView.clicks(vSendCode).throttleFirst(3, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        mPresenter.sendCode(vUserName.getText().toString());
                    }
                });
        vSure.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    vRegister.setEnabled(true);
                    vRegister.getDelegate().setBackgroundColor(
                            ContextCompat.getColor(mContext, R.color.colorPrimary));
                    vRegister.getDelegate().setBackgroundPressColor(
                            ContextCompat.getColor(mContext, R.color.base_press_color));
                } else {
                    vRegister.setEnabled(false);
                    vRegister.getDelegate().setBackgroundColor(
                            ContextCompat.getColor(mContext, R.color.c_cccccc));
                }
            }
        });
        vRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!checkParameter(false)) return;
                if (TextUtils.isEmpty(vInvite.getText())) {
                    showInviteDialog();
                } else {
                    mPresenter.register(vUserName.getText().toString(), vPassword.getText().toString(),
                            vInvite.getText().toString(), vVerificationCode.getText().toString());
                }
            }
        });
    }

    /**
     * 弹出没有填写推荐人提示框
     */
    private void showInviteDialog() {
        if (mDialog == null) {
            mDialog = new MaterialDialog(mContext)
                    .title("温馨提示")
                    .content("您还没有填写推荐人，是否去填写？")
                    .btnText("不用了，完成注册", "立即填写")
                    .autoDismiss(false);
            mDialog.setCancelable(false);
            mDialog.setOnBtnClickL(new OnBtnClickL() {
                @Override
                public void onBtnClick() {
                    mDialog.dismiss();
                    mPresenter.register(vUserName.getText().toString(),
                            vPassword.getText().toString(),
                            vInvite.getText().toString(),
                            vVerificationCode.getText().toString());
                }
            }, new OnBtnClickL() {
                @Override
                public void onBtnClick() {
                    mDialog.dismiss();
                    SoftInputUtil.openKeybord(vInvite, mContext);
                }
            });
        }
        if (!mDialog.isShowing())
            mDialog.show();
    }

    @Override
    public void byoyomi(Long aLong) {
        vSendCode.setText(aLong + "秒");
    }

    @Override
    public void changeByStatus(boolean status) {
        if (status) {
            vSendCode.setEnabled(false);
            vSendCode.setTextColor(ContextCompat.getColor(mContext, R.color.c_cccccc));
        } else {
            vSendCode.setText("获取验证码");//数据发送完后设置为原来的文字
            vSendCode.setEnabled(true);
            vSendCode.setTextColor(ContextCompat.getColor(mContext, R.color.color_base));
        }
    }

    @Override
    public boolean checkParameter(boolean checkCode) {
        String userName = vUserName.getText().toString();
        String password = vPassword.getText().toString();
        String code = vVerificationCode.getText().toString();
        String comfirmPwdStr = comfirmPwd.getText().toString();
        if (TextUtils.isEmpty(userName)) {
            ToastUtil.showTextViewPromptShort("手机号码不能为空");
            SoftInputUtil.openKeybord(vUserName, mContext);
            return false;
        } else if (VerifyUtil.checkPhoneNumber(userName)) {
            ToastUtil.showTextViewPromptShort("电话号码输入不正确");
            SoftInputUtil.openKeybord(vUserName, mContext);
            return false;
        }
        if (!checkCode) {
            if (TextUtils.isEmpty(password)) {
                ToastUtil.showTextViewPromptShort("密码不能为空");
                SoftInputUtil.openKeybord(vPassword, mContext);
                return false;
            }
            if (password.length() < 6) {
                ToastUtil.showTextViewPromptShort("密码不能小于6位数");
                SoftInputUtil.openKeybord(vPassword, mContext);
                return false;
            }
            if (TextUtils.isEmpty(comfirmPwdStr)) {
                ToastUtil.showTextViewPromptShort("确认密码不能为空");
                SoftInputUtil.openKeybord(comfirmPwd, mContext);
                return false;
            }
            if (!TextUtils.equals(password, comfirmPwdStr)) {
                ToastUtil.showTextViewPromptShort("两次密码输入不一致");
                SoftInputUtil.openKeybord(comfirmPwd, mContext);
                return false;
            }
            if (TextUtils.isEmpty(code)) {
                ToastUtil.showTextViewPromptShort("验证码不能为空");
                SoftInputUtil.openKeybord(vVerificationCode, mContext);
                return false;
            }
            if (!vSure.isChecked()) {
                ToastUtil.showTextViewPromptShort("必须同意使用协议");
                SoftInputUtil.hideSoftInput(this);
                return false;
            }
            if (TextUtils.isEmpty(vInvite.getText().toString())) {
                ToastUtil.showTextViewPromptShort("邀请人不能为空");
                return false;
            }
        }
        return true;
    }

    @Override
    public void setPresenter(RegisterContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onPause() {
        super.onPause();
        SoftInputUtil.hideSoftInput(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
            mDialog = null;
        }
    }
}
